package presentation.web;


import jaxcent.HtmlButton ;
import jaxcent.HtmlInputPassword ;
import jaxcent.HtmlInputText ;
import jaxcent.HtmlPara ;
import jaxcent.Jaxception ;
import presentation.UIUser ;
import util.NotFoundException ;
import domain.user.BadPasswordException ;


/**
 * The Class LoginPage.
 * @author Hadas
 */
public class LoginPage extends MainPage
{

	/** The username text field. */
	private HtmlInputText name;

	/** The password text field. */
	private HtmlInputPassword pass;

	/** The errors paragraph contains all error messages to user. */
	private HtmlPara errorsPara;

	/**
	 * Instantiates a new login page.
	 *
	 */
	public LoginPage()
	{
		super();
		try
		{
			this.pass = new HtmlInputPassword(this,"pass");
			this.name = new HtmlInputText(this,"name");
			this.setErrors();
			this.makeButton();
		}		
		catch (Exception e)
		{
			this.navigateToErrorPage(e, null);
		}
	}

	/**
	 * Login button handler.
	 * Handles with login to the system, wrong username or password etc.
	 */
	@SuppressWarnings("unchecked")
	protected void htmlButtonHandler()
	{
		String username = "";
		String password = "";
		try
		{
			this.errorsPara.setInnerHTML("");
			username = this.name.getValue();
			password = this.pass.getValue();
			//Case username or password fields are empty
			if ((username.equals("")) || (password.equals("")))
			{
				this.name.setValue("");
				this.pass.setValue("");
				this.errorsPara.setInnerHTML("Missing username or password!");
			}
			else
			{	//Login case
				UIUser me = (UIUser)this.getAllSessionData(false).get("user") ;
				final UIUser member = this.getForumLogic().login(username, password);
				this.getForumLogic().exitUser(me.getId()) ;
				this.getAllSessionData(false).put("user", member) ;
				this.navigateToPage(MainPage.HOME_PAGE) ;
			}
		}
		catch (final NotFoundException e)
		{	//User not in forum system
			try
			{
				this.name.setValue("");
				this.pass.setValue("");
				this.errorsPara.setInnerHTML("Username doesn't exist , Please register first.");
			}
			catch (Jaxception e1)
			{
				this.navigateToErrorPage("Error in forms components", null);
			}
		}
		catch (final BadPasswordException e)
		{	//Wrong password case
			try
			{
				this.pass.setValue("");
				this.errorsPara.setInnerHTML("Incorrect password!");
			}
			catch (Jaxception e1)
			{
				this.navigateToErrorPage(e, this.errorsPara);
			}
		}
		catch (Exception e)
		{
			this.navigateToErrorPage(e, null) ;
		}
	}//End htmlButtonHandler

	/**
	 * Create login button.
	 * Create correspond override onClick method
	 */
	private void makeButton(){
		try
		{
			new HtmlButton(this,"login"){
		        @Override
				protected void onClick() {
		        	LoginPage.this.htmlButtonHandler();
		        }
		    };
		}
		catch (Exception e)
		{
			this.navigateToErrorPage(e, null) ;
		}
	}

	/**
	 * Sets the errors paragraph with initialize empty text.
	 */
	private void setErrors()
	{
		try
		{
			this.errorsPara = new HtmlPara(this,"errorField");
			this.errorsPara.setInnerHTML("");
		}
		catch (Exception e)
		{
			this.navigateToErrorPage(e, this.errorsPara);
		}
	}
}

